//==================================================================
//  Copyright (C) 2006  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================///
///
///
///
//==================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "psys.h"

//==================================================================
int main( int argc, char *argv[] )
{
	if ( argc < 2 )
	{
		fprintf( stderr, "ERROR: Not enough params !" );
		exit( -1 );
	}

	FILE		*fp;
	const char	*in_fnamep = NULL;
	bool		param_add0ch = false;

	for (int i=1; i < argc; ++i)
	{
		if ( argv[i][0] != '-' )
		{
			in_fnamep = argv[i];
		}
		else
		{
			if ( argv[i][1] == '0' )
				param_add0ch = true;
		}
	}

	if NOT( in_fnamep )
	{
		fprintf( stderr, "ERROR: Missing filename !" );
		exit( -1 );
	}

	if NOT( fp = fopen( in_fnamep, "rb" ) )
	{
		fprintf( stderr, "ERROR: Cannot open %s in input.", in_fnamep );
		exit( -1 );
	}

	fseek( fp, 0, SEEK_END );
	int	len = ftell( fp );
	if ( len < 0 )
	{
		fprintf( stderr, "ERROR: Cannot measure the size of the file in input." );
		exit( -1 );
	}
	fseek( fp, 0, SEEK_SET );
	u_char	*datap = new u_char[ len ];
	if ( fread( datap, 1, len, fp ) < 0 )
	{
		fprintf( stderr, "ERROR: Cannot read the file in input." );
		exit( -1 );
	}
	fclose( fp );

	char	name[1024];
	int		name_len = strlen( in_fnamep );

	for (int i=name_len-1; i >= 0; --i)
	{
		if ( in_fnamep[i] == '/' || in_fnamep[i] == '\\' || in_fnamep[i] == ':' || i == 0 )
		{
			if ( i != 0 )
				++i;

			for (int j=i; j < name_len; ++j)
			{
				if ( isalnum( in_fnamep[j] ) )
					name[j-i] = in_fnamep[j];
				else
					name[j-i] = '_';
			}
			name[name_len-i] = 0;
			break;
		}
	}

	printf( "//==================================================================\n" );
	printf( "/// HEX of %s\n", name );
	printf( "///\n" );
	printf( "/// File automatically generated by pbin2hex\n" );
	printf( "/// pbin2hex version: "__DATE__","__TIME__"\n" );
	printf( "//==================================================================\n" );
	printf( "\n" );
	printf( "static unsigned char _%s[] = {\n", name );
	for (int i=0; i < len;)
	{
		if ( i+16 < len )
		{
			printf( "0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,"
					"0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,\n",
					datap[i+0x00],datap[i+0x01],datap[i+0x02],datap[i+0x03],datap[i+0x04],datap[i+0x05],datap[i+0x06],datap[i+0x07],
					datap[i+0x08],datap[i+0x09],datap[i+0x0a],datap[i+0x0b],datap[i+0x0c],datap[i+0x0d],datap[i+0x0e],datap[i+0x0f] );

			i += 16;
		}
		else
		{
			for (int j=i; j < len; ++j)
			{
				printf( "0x%02x,", datap[j] );
			}
			printf( "\n" );
			break;
		}
	}

	if ( param_add0ch )
		printf( "0x00\n" );
	
	printf( "}; \n" );

}